package com.lj.api.common;


import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.PrintWriter;


public class Result<T> {
    private String msg;
    private int retCode;
    private T data;

    private Result() {
        this.retCode = 1;
        this.msg = "";
    }

    private Result(T data) {
        this.retCode = 1;
        this.msg = "";
        this.data = data;
    }
    private Result(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.retCode = cm.getRetCode();
        this.msg = cm.getMessage();
        this.data = (T) new String("");
    }

    public Result(int retCode, T data,String message) {
        this.msg = message;
        this.retCode = retCode;
        this.data = data;
    }


    /**
     * 成功时候的调用
     * @return
     */
    public static <T> String ok(T data){
        return JsonUtils.toJsonString(new Result<T>(data));
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static String ok(){
        return JsonUtils.toJsonString(new Result());
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static <T> Result<T> success(T data){
        return  new Result<T>(data);
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> Result<T> success(){
        return (Result<T>) success("");
    }

    /**
     * 失败时候的调用
     * @return
     */
    public static String fail(CodeMsg cm){
        return  JsonUtils.toJsonString(new Result(cm));
    }

    public static <T> String msg(int retCode,T data,String message){
        return JsonUtils.toJsonString(new Result(retCode,data,message));
    }

    public static <T> String okMsg(T data,String message){
        return JsonUtils.toJsonString(new Result(1,data,message));
    }

    /**
     * 失败时候的调用
     * @return
     */
    public static String error(CodeMsg cm){
        return  JsonUtils.toJsonString(new Result(cm));
    }

    /**
     * 失败时候的调用,扩展消息参数
     * @param cm
     * @param msg
     * @return
     */
    public static <T> Result<T> error(CodeMsg cm,String msg){
        cm.setMessage(cm.getMessage()+"--"+msg);
        return new Result<T>(cm);
    }
    public T getData() {
        return data;
    }
    public String getMessage() {
        return msg;
    }
    public int getRetCode() {
        return retCode;
    }


    /**
     * 直接输出内容的简便函数.kkkkkkk
     *
     * eg. render("text/plain", "hello", "encoding:GBK"); render("text/plain",
     * "hello", "no-cache:false"); render("text/plain", "hello", "encoding:GBK",
     * "no-cache:false");
     *
     * @param headers
     *            可变的header数组，目前接受的值为"encoding:"或"no-cache:",默认值分别为UTF-8和true.
     */
    public static void render(final HttpServletResponse response, final String contentType, final String content,
                              final String... headers) {

        String ENCODING_PREFIX = "encoding";
        String NOCACHE_PREFIX = "no-cache";
        String ENCODING_DEFAULT = "UTF-8"; //
        boolean NOCACHE_DEFAULT = true;

        try {
            // 分析headers参数
            String encoding = ENCODING_DEFAULT;
            boolean noCache = NOCACHE_DEFAULT;
            for (String header : headers) {
                String headerName = StringUtils.substringBefore(header, ":");
                String headerValue = StringUtils.substringAfter(header, ":");

                if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
                    encoding = headerValue;
                } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
                    noCache = Boolean.parseBoolean(headerValue);
                } else
                    throw new IllegalArgumentException(headerName + "不是一个合法的header类型");
            }

            // 设置headers参数
            String fullContentType = contentType + ";charset=" + encoding;

            response.setContentType(fullContentType);
            if (noCache) {
                /*response.setContentType("text/plain");*/
                response.setHeader("Cache-Control", "no-cache");
                response.setCharacterEncoding("UTF-8");
            }

            PrintWriter writer = response.getWriter();
            writer.write(content);
            response.getWriter().flush();
            writer.close();
        } catch (IOException e) {
            System.out.println("Error");
            e.printStackTrace();
        }

    }


}